<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="学号">
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select allowClear v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="12">
              <a-form-item
                label="学生来源"
              >
                <a-select :allowClear="true" v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="12">
              <a-form-item label="培养单位">
                <a-select allowClear v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="专业">
                <a-select v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                  <a-select-option v-for="(i,index) in selectMenu.zyList" :key="index" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="年级">
                <a-select allowClear v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学籍状态">
                <a-select allowClear v-model="queryParam.xjztm">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="batchReset">重置培养计划</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
              <a-button @click="queryParam = {};$refs.table.refresh()">重置</a-button>

            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :rowSelection="options.rowSelection"
        :alert="{show: true}"
        :pageSize="parameter.pageSize"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="xh" slot-scope="text, record">
          <a @click="showDetail(record)">{{text}}</a>
        </span>
      </s-table>
    </a-card>

    <plan-info ref="plan" :visible="planVisible" @cancel="planVisible = false"/>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import planInfo from '../components/planInfo'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {
        xjztm: ''
      },
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学号',
          dataIndex: 'xh',
          scopedSlots: { customRender: 'xh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '年级',
          dataIndex: 'sznjmc'
        },
        {
          title: '培养单位',
          dataIndex: 'dwmc'
        },
        {
          title: '专业',
          dataIndex: 'zymc'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '导师',
          dataIndex: 'dsxm'
        },
        {
          title: '审核状态',
          dataIndex: 'pyjhztmc',
          width: '160px'
        }
        // {
        //   title: '审核状态',
        //   dataIndex: 'operation',
        //   width: '160px',
        //   scopedSlots: { customRender: 'operation' }
        // }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.planReset(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      selectedRows: [],
      options: {
        rowSelection: {
          onChange: this.onSelectChange
        }
      },
      planVisible: false
    }
  },
  components: {STable, planInfo},
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0073: state => state.app['TYPE0073'], // 学籍状态
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
      })
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zyList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zyList', [])
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
    },
    showDetail (t) {
      this.planVisible = true
      this.$refs.plan.getInfo(t.id)
    },
    batchReset () {
      if (!this.selectedRows.length) {
        this.$message.warning('请选择需要重置的记录')
        return
      }
      this.$confirm({
        title: '确定重置所选中的记录?',
        content: '当您点击确定按钮后，这些记录将会被重置',
        centered: true,
        onOk: () => {
          let ids = []
          this.selectedRows.forEach(plan => {
            ids.push(plan.id)
          })
          this.$api.cultivate.resetPlan({ids: ids.join(',')}).then(res => {
            this.$message.success(res.msg)
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    }
  }
}
</script>

<style>

</style>
